<template>
	<jk-dialog
		v-model="showDialog"
		:title="`${dataId ? '编辑' : '新增'}-保养项目`"
		width="700px"
		:show-content-loading="showLoading"
		:confirm-btn-loading="confirmBtnLoading"
		append-to-body
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-confirm="onConfirmBtn"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" :show-message="false">
			<el-row class="flex-start-center-wrap">
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="编号" prop="code">
						<el-input v-model="ruleForm.code" placeholder="编号" @blur="onBlurCode" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="名称" prop="name">
						<el-input v-model="ruleForm.name" placeholder="名称" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="工序">
						<jk-select v-model="ruleForm.processId" :option-list="processList" size="medium" class="width-100-percent" placeholder="工序"></jk-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="保养类别" prop="upkeepTypeId">
						<jk-select v-model="ruleForm.upkeepTypeId" :option-list="upkeepTypeList" size="medium" class="width-100-percent" placeholder="保养类别" @change="onTypeChange"></jk-select>
					</el-form-item>
				</el-col>
				<el-col v-if="ruleForm.upkeepTypeCode ? ruleForm.upkeepTypeCode.toUpperCase() === 'RHJY' : false" :span="12">
					<el-form-item class="margin-bottom-10" label="规格">
						<el-input v-model="ruleForm.upkeepModelName" placeholder="规格" />
					</el-form-item>
				</el-col>
				<el-col v-if="ruleForm.upkeepTypeCode ? ruleForm.upkeepTypeCode.toUpperCase() === 'RHJY' : false" :span="12">
					<el-form-item class="margin-bottom-10" label="工具">
						<el-input v-model="ruleForm.upkeepToolName" placeholder="工具" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="序号">
						<jk-number-input v-model="ruleForm.sortNum" placeholder="序号" />
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item class="" label="备注">
						<el-input v-model="ruleForm.remark" autosize type="textarea" placeholder="请输入" />
					</el-form-item>
				</el-col>
			</el-row>
			<jk-divider-card title="保养周期" icon="el-icon-date" placement="center" class="margin-top-20 margin-bottom-10">
				<el-row class="flex-start-center-wrap">
					<el-col :span="24">
						<el-form-item class="margin-bottom-10" label="周期单位">
							<el-radio-group v-model="ruleForm.cycleUnit">
								<el-radio-button :label="0">时</el-radio-button>
								<el-radio-button :label="1">天</el-radio-button>
								<el-radio-button :label="2">月</el-radio-button>
								<el-radio-button :label="3">年</el-radio-button>
								<el-radio-button :label="4">产量(米)</el-radio-button>
								<el-radio-button :label="5">产量(公斤)</el-radio-button>
								<el-radio-button :label="6">产量(吨)</el-radio-button>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="margin-bottom-0" label="最小周期" prop="cycleMin">
							<el-input v-model="ruleForm.cycleMin" placeholder="最小周期" />
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item class="margin-bottom-0" label="最大周期" prop="cycleMax">
							<el-input v-model="ruleForm.cycleMax" placeholder="最大周期" />
						</el-form-item>
					</el-col>
				</el-row>
			</jk-divider-card>
		</el-form>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import {
        spinUpkeepItemCreate,
        spinUpkeepItemUpdate,
        spinUpkeepItemGet
    } from '../../../../api/basicData/upkeepItem';
    import { spinProcessPage } from '../../../../api/technology/process';
    import { machineManagerUpkeepTypePage } from '../../../../api/machine/upkeepCategory';
    export default {
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        mixins: [calHeight],
        data() {
            return {
                upkeepTypeList: [],
                processList: [],
                showDialog: false,
                showLoading: true,
                confirmBtnLoading: false,
                ruleForm: {
                    id: null,
                    code: '',
                    name: '',
                    cycleMax: null,
                    cycleMin: null,
                    upkeepTypeId: null,
                    upkeepTypeCode: '',
                    upkeepTypeName: '',
                    cycleUnit: 1,
                    sortNum: 1,
                    processId: null,
                    processCode: '',
                    processName: '',
                    remark: '',
                    upkeepModelName: '',
                    upkeepToolName: ''
                },
                rules: {
                    upkeepTypeId: [{ required: true, trigger: 'change' }],
                    code: [{ required: true, trigger: 'change' }],
                    cycleUnit: [{ required: true, trigger: 'change' }],
                    name: [{ required: true, trigger: 'change' }],
                    processId: [{ required: true, trigger: 'change' }],
                    cycleMax: [{ required: true, trigger: 'change' }],
                    cycleMin: [{ required: true, trigger: 'change' }]
                }
            };
        },
        methods: {
            onTypeChange(e) {
                const item = this.upkeepTypeList.find(x => x.id === e) || {};
                this.$set(this.ruleForm, 'upkeepTypeCode', item.code);
                this.$set(this.ruleForm, 'upkeepTypeName', item.name);
            },
            onBlurCode(e) {
                this.$set(this.ruleForm, 'code', this.ruleForm.code ? this.ruleForm.code.toUpperCase() : '');
            },
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                const processItem = this.processList.find(x => x.id === params.processId) || {};
                params.processCode = processItem.code;
                params.processName = processItem.name;

                const upkeepTypeItem = this.upkeepTypeList.find(x => x.id === params.upkeepTypeId) || {};
                params.upkeepTypeCode = upkeepTypeItem.code;
                params.upkeepTypeName = upkeepTypeItem.name;

                return params;
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        this.confirmBtnLoading = true;
                        // 新增
                        if (!this.dataId) {
                            this.ruleForm.id = null;
                            spinUpkeepItemCreate(this.getParamData()).then(res => {
                                this.confirmBtnLoading = false;
                                if (res.data.status === 200) {
                                    this.$tipsBar(this, 'save');
                                    this.$emit('on-finish');
                                }
                            });
                            return;
                        }
                        // 编辑
                        spinUpkeepItemUpdate(this.getParamData()).then(res => {
                            this.confirmBtnLoading = false;
                            if (res.data.status === 200) {
                                this.$tipsBar(this, 'save');
                                this.$emit('on-finish');
                            }
                        });
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                return spinUpkeepItemGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        this.ruleForm = res.data.res || {};
                    }
                });
            },
            getProcessListRequest() {
                return spinProcessPage({ auditState: 1 }).then(res => {
                    if (res.data.status === 200) {
                        this.processList = res.data.res || [];
                    }
                });
            },
            getUpkeepTypeListRequest() {
                return machineManagerUpkeepTypePage({ auditState: 1 }).then(res => {
                    if (res.data.status === 200) {
                        this.upkeepTypeList = res.data.res || [];
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                await this.getUpkeepTypeListRequest();
                await this.getProcessListRequest();
                if (this.dataId) await this.getDetailRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
